@extends('admin.layout.base')
@section('title', '广告列表页面')
@section('pluginstyle')
<meta name="csrf-token" content="{{ csrf_token() }}" />
@endsection
@section('content')
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            广告列表页面
        </h1>
        <ol class="breadcrumb">
            <li><a href="{{ url('/admin') }}"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li class="active">广告管理</li>
        </ol>
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-header">
                        <div class="btn-group">
                            <button class="btn btn-default isopenmore" data-status='0'>批量开启</button>
                            <button class="btn btn-default isopenmore" data-status='1'>批量关闭</button>
                            <button class="btn btn-default" id="delmore">批量删除</button>
                            <a class="btn btn-success" href="{{ url('/admin/ads/create') }}">添加广告</a>
                        </div>
                        <div class="box-tools">
                            <form action="{{ url('/admin/ads') }}" method="get">
                            <div class="input-group" style="width: 150px;">
                                <input type="text" name="goods_name" value="{{ old('goods_name') }}" class="form-control pull-right" placeholder="商品名称">
                                <div class="input-group-btn">
                                    <button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
                                </div>
                            </div>
                            </form>
                        </div>
                    </div>
                    <!-- /.box-header -->
                    <div class="box-body table-responsive no-padding">
                        <table class="table table-hover table-bordered">
                            <tr>
                                <th>
                                <input type="checkbox" name="" id="checkall"/>#
                                </th>
                                <th>广告标题</th>
                                <th>广告位置</th>
                                <th>广告图片</th>
                                <th>是否显示</th>
                                <th>操作</th>
                            </tr>
                            @foreach($ads as $ad)
                            <tr>
                                <td>
                                    <input type="checkbox" name="" class="subcheck" value="{{$ad->id}}" />{{ $ad->id }}
                                </td>
                                <td>{{ $ad->ads_title }}</td>
                                <td>
                                    {{ $ad->ads_position }}
                                </td>
                                <td>
                                    <img src="{{ $ad->ads_img }}" alt="{{ $ad->ads_title }}" height="40" />
                                </td>
                                <td>
                                    <select class="ads_status" name="ads_status" data-id="{{ $ad->id }}" class="status">
                                        <option value="1" {{ $ad->ads_status==1 ? 'selected' : '' }} >开启</option>
                                        <option value="0" {{ $ad->ads_status==0 ? 'selected' : '' }} >关闭</option>
                                    </select>
                                </td>
                                <td>
                                    <a href="{{ url("/admin/ads/{$ad->id}/edit") }}" class="btn btn-info">编辑</a>
                                    <a href="{{ url('/admin/ads',['id'=>$ad->id]) }}" class="btn btn-danger del">删除</a>
                                </td>
                            </tr>
                            @endforeach
                        </table>
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer">
                        {!! $ads->appends(['ads_title'=>old('ads_title')])->render() !!}
                    </div>
                </div>
                <!-- /.box -->
            </div>
        </div>
    </section>
    <!-- /.content -->
</div>
@endsection
@section('pluginscript')
<!-- Slimscroll -->
<script src="{{ asset('/dist/admin/plugins/slimScroll/jquery.slimscroll.min.js') }}"></script>
<!-- FastClick -->
<script src="{{ asset('/dist/admin/plugins/fastclick/fastclick.js') }}"></script>
<script type="text/javascript">
    $(function(){
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        // 显示隐藏
        $('.ads_status').change(function(){
            var url = '{{ url("/admin/ads/isopen") }}';
            var data = {};
            data.id = $(this).attr('data-id');
            data.status = $(this).val();
            
            $.post(url,data, function(msg){
                if(msg.status != '1'){
                    alert(msg.info);
                }
                location.reload();
            });
        });
        
        // 删除
        $('.del').click(function(){
            var url = $(this).attr('href');
            var tr = $(this).parents('tr');
            var result = confirm('确定要删除？');
            
            if(result){
                $.ajax({
                    url: url,
                    type:'DELETE',
                    success:function(msg){
                        if(msg.status == '1'){
                            tr.remove();
                        } else {
                            alert(msg.info);
                        }
                    }
                });
            }
            return false;
        });
        
        $('#delmore').click(function(){
            var ids = getIds();
            if(ids.length == 0){
                alert('请至少选择一个');
                return false;
            }
            
            $.ajax({
                url: "{{ url('/admin/ads/delete') }}",
                type:'POST',
                data:{id:ids},
                success:function(msg){
                    alert(msg.info);
                    if(msg.status == '1'){
                        location.reload();
                    }
                }
            });
        });
        
        $('.isopenmore').click(function(){
            var ids = getIds();
            var status = $(this).attr('data-status');
            if(ids.length == 0){
                alert('请至少选择一个');
                return false;
            }
            
            $.ajax({
                url: "{{ url('/admin/ads/isopen') }}",
                type:'POST',
                data:{id:ids,status:status},
                success:function(msg){
                    alert(msg.info);
                    if(msg.status == '1'){
                        location.reload();
                    }
                }
            });
        });
        // 获取选中的id
        function getIds(){
            var ids = [];
            $('.subcheck:checked').each(function(i){
               ids[i] = $(this).val();
            });
            return ids;
        }
        
        $('#checkall').click(function(){
            checked('#checkall','.subcheck');
        });
        $('.subcheck').click(function(){
            if($(this)[0].checked === false){
                $('#checkall')[0].checked = false;
            }
        });
    });
</script>
@endsection